﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using System;
using System.Net;
using System.Text;

namespace CatUtilsDll.Utils.Currency.WebUtils
{
    /// <summary>
    /// 保存记忆COOKIES的webclient增强方法
    /// </summary>
    public class CWebclient : WebClient
    {
        /// <summary>
        ///  保存记忆COOKIES的webclient增强方法
        /// </summary>
        public CWebclient()
        {
            this.TempcookieContainer = new CookieContainer();
        }

        /// <summary>
        ///  保存记忆COOKIES的webclient增强方法
        /// </summary>
        /// <param name="cookie"></param>
        public CWebclient(CookieContainer cookie)
        {
            this.TempcookieContainer = cookie;
        }

        /// <summary>
        /// 保存的COOKIES
        /// </summary>
        public CookieContainer TempcookieContainer { get; set; }

        /// <summary>
        /// 重新赋值cookies
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                HttpWebRequest httpWebRequest = request as HttpWebRequest;
                httpWebRequest.CookieContainer = TempcookieContainer;
            }
            return request;
        }

        /// <summary>
        /// 获取301/302转向网址
        /// </summary>
        /// <returns></returns>
        public string GetMoveUrl(string address)
        {
            WebRequest request = base.GetWebRequest(new Uri(address));

            if (request is HttpWebRequest)
            {
                try
                {
                    HttpWebRequest httpWebRequest = request as HttpWebRequest;

                    httpWebRequest.CookieContainer = TempcookieContainer;
                    HttpWebResponse httpRes = (HttpWebResponse)httpWebRequest.GetResponse();

                    return httpRes.ResponseUri.AbsoluteUri;
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                }
            }

            return "";
        }

        /// <summary>
        /// 获取源码
        /// </summary>
        /// <param name="Link"></param>
        /// <returns></returns>
        public string GetHtml(string Link)
        {
            return GetHtml(Link, WebHelper.PcWebHeader(), Encoding.UTF8);
        }

        /// <summary>
        /// 获取源码
        /// </summary>
        /// <param name="Link"></param>
        /// <param name="webHeaderCollection"></param>
        /// <returns></returns>
        public string GetHtml(string Link, WebHeaderCollection webHeaderCollection)
        {
            return GetHtml(Link, webHeaderCollection, Encoding.UTF8);
        }

        /// <summary>
        /// 获取源码
        /// </summary>
        /// <param name="Link"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public string GetHtml(string Link, Encoding encoding)
        {
            return GetHtml(Link, WebHelper.PcWebHeader(), encoding);
        }

        /// <summary>
        /// 获取源码
        /// </summary>
        /// <param name="Link"></param>
        /// <param name="webHeaderCollection"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public string GetHtml(string Link, WebHeaderCollection webHeaderCollection, Encoding encoding)
        {
            string ReqText = "";
            this.Headers = webHeaderCollection;

            try
            {
                byte[] responseData = this.DownloadData(Link);
                ReqText = TextHelper.ByteToString(responseData, encoding);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return ReqText;
        }

        /// <summary>
        /// 发送post
        /// </summary>
        /// <param name="PostUrl"></param>
        /// <param name="PostData"></param>
        /// <returns></returns>
        public string SendPost(string PostUrl, string PostData)
        {
            WebHeaderCollection webHeaderCollection = WebHelper.PcWebHeader();
            webHeaderCollection.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            return SendPost(PostUrl, PostData, webHeaderCollection, Encoding.UTF8);
        }

        /// <summary>
        ///  发送post
        /// </summary>
        /// <param name="PostUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="webHeaderCollection"></param>
        /// <returns></returns>
        public string SendPost(string PostUrl, string PostData, WebHeaderCollection webHeaderCollection)
        {
            return SendPost(PostUrl, PostData, webHeaderCollection, Encoding.UTF8);
        }

        /// <summary>
        /// 发送post
        /// </summary>
        /// <param name="PostUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public string SendPost(string PostUrl, string PostData, Encoding encoding)
        {
            return SendPost(PostUrl, PostData, WebHelper.PcWebHeader(), encoding);
        }

        /// <summary>
        /// 发送post 例如pixiv_id={0}&amp;captcha=
        /// </summary>
        /// <param name="PostUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="webHeaderCollection"></param>
        /// <param name="encoding">返回HTML解码用</param>
        /// <returns></returns>
        public string SendPost(string PostUrl, string PostData, WebHeaderCollection webHeaderCollection, Encoding encoding)
        {
            byte[] PostDataByte = TextHelper.StringToByte(PostData, Encoding.UTF8);
            this.Headers = webHeaderCollection;

            this.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            string ReqText = "";
            try
            {
                byte[] responseData = this.UploadData(PostUrl, "POST", PostDataByte);
                ReqText = TextHelper.ByteToString(responseData, encoding);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }

            return ReqText;
        }
    }
}